package com.zingrow.web.configure.channel.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.StringUtil;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.common.utils.AnalyzeResult;
import com.zingrow.web.configure.channel.mapper.ChannelMapper;
import com.zingrow.web.configure.channel.model.Channel;
import com.zingrow.web.configure.channel.service.IChannelService;
import com.zingrow.web.configure.domain.mapper.DomainMapper;
import com.zingrow.web.configure.domain.model.Domain;
@Service
public class ChannelService extends BaseService<Channel> implements IChannelService {
	@Autowired
	private ChannelMapper channelMapper;
	@Autowired
	private DomainMapper domainMapper;

	@Override
	public void initMapper(){
		this.baseMapper = channelMapper;
	}
	@Override
	/**
     * 批量删除
     */
	public boolean batchDeletes(List delList) {
	  return channelMapper.batchDeletes(delList)>0;
	}
		
		
    
	//模糊查询
	@Override
	public List<Channel> queryByParams(PageView pageView,String channel_url,
			String channel_name,String channel_type,Integer is_exprot) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("channel_url", channel_url);
		map.put("channel_name", channel_name);
		map.put("channel_type", channel_type);
		map.put("is_exprot", is_exprot);
		return channelMapper.queryByParams(map);
	}
	//新增
	@Override
	public boolean addChannel(Channel channel){
		//给状态赋值为1
		if (null==channel.getIs_delete()) {
			channel.setIs_delete(1);
		}
		//导出状态默认值为0
		if(null==channel.getIs_exprot()) {
			channel.setIs_exprot(0);
		}
		return channelMapper.addChannel(channel) > 0;
	}
	//修改
	@Override
	public boolean modifyChannel(Channel channel){
		return channelMapper.modifyChannel(channel)>0;
	}
	//修改显示
	@Override
	public boolean modifyDisplay(int display,List<String> displaylist){
		if(display==1)
		{
			return channelMapper.updatadisplay(displaylist)>0;
		}
		else
		{
			return channelMapper.updatadisplayno(displaylist)>0;
		}
		}
	//删除
	@Override
	public boolean deleteChnnel(Integer channel_id){
		return channelMapper.deleteChnnel(channel_id)>0;
	}
	/***查询同名
	 * yzx*/
	@Override
	public boolean queryByName(Integer channel_id,String channel_url,String channel_name){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel_id", channel_id);
		map.put("channel_url", channel_url);
		map.put("channel_name", channel_name);
		return channelMapper.queryByName(map)>0;
	}
	//单个查询
	@Override
	public Channel queryById(Integer channel_id){
		Channel channel = channelMapper.queryById(channel_id);
		return channel;
	}
	//查询全部的接口
	@Override
	public List<Channel> queryByIdName(PageView pageView){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		return channelMapper.queryByIdName(map);
	}
	//根据list集合中的id查询接口
	@Override
	public List<Channel> queryChannelByIdlist(List<String> idlist){
		if(null != idlist){
			return channelMapper.queryChannelByIdlist(idlist);
		}
		return null;
	}
	//导出
	@Override
	public List<Channel> queryChannelbyid(List<Integer> ids) {
		List<Channel> clist = new ArrayList<Channel>();
		//调用方法
		clist = channelMapper.queryChannelbyid(ids);
			if(clist.size()>0){
				channelMapper.modifyDomainIsexprot(ids);

		}
		//判断
		if(clist != null && clist.size() > 0){
			for (Channel channel : clist) {
				if (null == channel.getDomain_delete()) {
					channel.setDomain_name("["+channel.getDomain_name()+"]");
				} else if (channel.getDomain_delete()==0) {
					channel.setDomain_name("["+channel.getDomain_name()+"]已被删除");
				}
				//判断
				if("News".equals(channel.getChannel_type())){
					channel.setChannel_type("新闻");
				}else if("EPaper".equals(channel.getChannel_type())){
					channel.setChannel_type("电子报");
				}else if("ResearchReport".equals(channel.getChannel_type())){
					channel.setChannel_type("研报");
				}else if("PublicNotice".equals(channel.getChannel_type())){
					channel.setChannel_type("公告");
				}else if("WeChat".equals(channel.getChannel_type())){
					channel.setChannel_type("微信");
				}else if("Weibo".equals(channel.getChannel_type())){
					channel.setChannel_type("微博");
				}else if("BBS".equals(channel.getChannel_type())){
					channel.setChannel_type("论坛");
				}else if("BLOG".equals(channel.getChannel_type())){
					channel.setChannel_type("博客");
				}else if("Live".equals(channel.getChannel_type())){
					channel.setChannel_type("视频");
				}else{
					channel.setChannel_type("未知");
				}
				if(channel.getIs_delete()==0){
					channel.setIs_deletes("停用");
				}else if(channel.getIs_delete()==1){
					channel.setIs_deletes("启用");
				}else{
					channel.setIs_deletes("未知");
				}
			}
		}
		return clist;
	}
	
    @Override
    public List<Channel> queryChannelByType(PageView pageView,String channel_type,String channel_name) {
        Map <String,Object> map = new HashMap<String,Object>();
        map.put("paging", pageView);
        map.put("channel_type", channel_type);
        map.put("channel_name", channel_name);
        return channelMapper.queryChannelByType(map);
    }

	//导入
    @Override
	public void analyzeExcelData(File file,AnalyzeResult<Channel> result)throws Exception{
		List<Channel> list = new ArrayList<Channel>();
		//解析文件
		Workbook work = Workbook.getWorkbook(file);
		//获取第一个工作表
		Sheet sheet = work.getSheet(0);
		int cols = 0;
		int rows = 0;
		//判断工作表获取是否正常
		if(CommonUtils.isObjectNotNull(sheet)){
			//得到列的数量
			cols = sheet.getColumns();
			//得到行的数量
			rows = sheet.getRows();
			if(cols<=1||rows<=1){
				result.setResult(false, "未检测到数据", new ArrayList<Channel>());
			}else if(rows>=9999999){
				result.setResult(false, "导入数量过多，请拆分数据分多次导入" + rows, new ArrayList<Channel>());
			}
		}else{
			//可能是没有sheet,也可能是其他原因
			result.setResult(false, "Excel工作表格格式错误", new ArrayList<Channel>());
		//判断，数据不合法直接返回
		}if(!result.isSuccess()){
			return;
		}
		//获取正常，进行一下判断
		for (int i = 1; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				String channel_url = sheet.getCell(j++, i).getContents().trim();
				String channel_type = sheet.getCell(j++, i).getContents().trim();
				String channel_name = sheet.getCell(j++, i).getContents().trim();
				
				//判断数据合法
				if (channel_url.length()<0 || channel_url.length()>250) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
				}
				if (StringUtils.isBlank(channel_url)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
				}
				if (!StringUtils.isBlank(channel_type)) {
					if ("新闻".equals(channel_type)||"News".equals(channel_type)) {
						channel_type = "News";
					} else if ("电子报".equals(channel_type)||"EPaper".equals(channel_type)) {
						channel_type = "EPaper";
					} else if ("研报".equals(channel_type)||"ResearchReport".equals(channel_type)) {
						channel_type = "ResearchReport";
					} else if ("公告".equals(channel_type)||"PublicNotice".equals(channel_type)) {
						channel_type = "PublicNotice";
					} else if ("微信".equals(channel_type)||"WeChat".equals(channel_type)) {
						channel_type = "WeChat";
					} else if ("微博".equals(channel_type)||"Weibo".equals(channel_type)) {
						channel_type = "Weibo";
					} else if ("论坛".equals(channel_type)||"BBS".equals(channel_type)) {
						channel_type = "BBS";
					} else if ("博客".equals(channel_type)||"BLOG".equals(channel_type)) {
						channel_type = "BLOG";
					} else if ("视频".equals(channel_type)||"Live".equals(channel_type)) {
						channel_type = "Live";
					} else {
						result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
					}
				} else {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
				}
				if (channel_name.length()<0 || channel_name.length() > 100) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
				}
				if (StringUtils.isBlank(channel_name)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Channel>());
				}
				// 以上判断通过，添加数据
				if (result.isSuccess()) {
					Channel channel = new Channel(null, null, channel_url,
							channel_type, channel_name);
						list.add(channel);
				} else {
					return;
				}
			}
		}
		result.setResult(true, null, list);
	}
}
